<template>
    <div class="container-common manage-score-outline">
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item>成绩管理</el-breadcrumb-item>
            <el-breadcrumb-item>成绩概览</el-breadcrumb-item>
        </el-breadcrumb>
        <el-button class="export-score" type="primary" @click="exportScore">导出成绩</el-button>
        <el-table
                :data="tableData"
                border
                style="width: 100%">
            <el-table-column
                    prop="id"
                    label="学号">
            </el-table-column>
            <el-table-column
                    prop="name"
                    label="姓名">
            </el-table-column>
            <template v-if="tableCol && tableCol.length">
                <el-table-column v-for="item in tableCol"
                                 :prop="item.id"
                                 :label="item.name"
                                 :key="item.id">
                </el-table-column>
            </template>
        </el-table>
    </div>
</template>

<script>
    import Api from '@utils/api';
    import { mapGetters } from 'vuex';

    export default {
        name: "outline",
        data () {
            return {
                tableData: [],
                tableCol: []
            }
        },
        computed: {
            ...mapGetters([
                'getCurrentCourseId'
            ])
        },
        methods: {
            getTableData () {
                Api({
                    method: 'get',
                    url: '/getScoreList',
                    params: {
                        course_id: this.getCurrentCourseId
                    }
                }).then(res => {
                    if (res.data && res.data.data) {
                        if (res.data.data.col[0]) {
                            let col = res.data.data.col[0];
                            this.tableCol = Object.keys(col).map(item => {
                                return {
                                    id: item,
                                    name: col[item]
                                }
                            })
                        }

                        this.tableData = res.data.data.row.map(item => {
                            let param = {
                                id: item.id,
                                name: item.name,
                            };

                            return Object.assign({}, param, item.tasks[0]);
                        });
                    }
                })
            },
            exportScore () {
                window.open(`http://39.105.144.7:5002/api/score/export?course_id=${this.getCurrentCourseId}`, '_self');
            }
        },
        created () {
            this.getTableData();
        }
    }
</script>

<style type="text/scss" lang="scss">
    .manage-score-outline {
        .export-score {
            margin-bottom: 20px;
        }
    }
</style>